package com.sky.aion.redis;

import com.sky.aion.AionConstant;
import com.sky.aion.utils.StatusEnum;
import redis.clients.jedis.*;

/**
 *  java连接redis
 * Created by sunzhg on 2016/8/8.
 */
public class JedisUtils {
    private static Jedis jedis;

    private JedisUtils() {
    }

    public static Jedis getJedis(){
        if (jedis==null){
            JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
            jedis = pool.getResource();
        }
        return jedis;
    }

    public static Jedis getNewJedis(){
        JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");

        return pool.getResource();
    }

    public static void setExpireKey(String key,String value,int expire){
        jedis=getJedis();
        jedis.set(key, value);
        jedis.expire(key, expire);
        //System.out.println("setkey："+jedis.set(key, value));
        //System.out.println("设置过期时间："+jedis.expire(key, expire));
    }

    public static void setExpireKey(StatusEnum speakEnum){
        jedis=getJedis();
        jedis.set(speakEnum.getRedisCode(), speakEnum.getMsg());
        jedis.expire(speakEnum.getRedisCode(), speakEnum.getRedisExpire());

    }

    public static void setKey(String key,String value){
        jedis=getJedis();
        jedis.set(key, value);
    }

    public static String getKey(String key){
        jedis=getJedis();
        if (jedis.exists(key)){
            return jedis.get(key);
        }
        return "";
    }



    public static void lpush(String key,String str){
        jedis=getJedis();
        jedis.lpush(key,str);
    }

    public static void rpush(String key,String str){
        jedis=getJedis();
        jedis.rpush(key,str);
    }

    public static String rpop(String key){
        jedis=getJedis();
        return jedis.rpop(key);
    }

    public static boolean isLlen(String key){
        jedis=getJedis();
        Long len=jedis.llen(key);
        if (len==null) return false;
        return len>0;
    }

    public static void delAll(){
        jedis=getJedis();
        for (StatusEnum statusEnum:StatusEnum.values()){
            if(jedis.exists(statusEnum.getRedisCode())){
                jedis.del(statusEnum.getRedisCode());
            }
        }
        /*if (jedis.exists(AionConstant.SPEAKLIST)){
            jedis.del(AionConstant.SPEAKLIST);
        }*/
    }

    public static void main(String[] args) {
        jedis=getJedis();
        System.out.println(jedis.llen(AionConstant.SPEAKLIST)>0);
        delAll();
    }




}
